//
//  NewPersonalCenterViewController.m
//  suRead
//
//  Created by lidi on 2018/1/3.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "NewPersonalCenterViewController.h"
#import "NewPersonalCenterCell.h"
#import "UIAlertView+Blocks.h"
#import "AccountManager.h"
#import "UIApplication+VisibleViewController.h"

#import "LoginViewController.h"
#import "MyCommentsViewController.h"
#import "MyMessageViewController.h"
#import "ModalViewController.h"
#import "MyWelfareViewController.h"
#import "NewsWebViewController.h"

#import "NewPersonalCenterHeaderView.h"
#import "PersonalEditInformationViewController.h"
#import "HotlineViewController.h"
#import "MyHistoryViewController.h"
#import "MyLikeViewController.h"

#import "ServiceUser.h"
#import "AppDelegate.h"
#import "Masonry.h"
#import "ReportListViewController.h"
#import "ServiceUserNew.h"

typedef NS_ENUM(NSInteger, CellType) {
    CellType_DB = 0,
    CellType_RMTFM,
    CellType_WDXX,
    CellType_FLSC,
    CellType_WDDD,
    CellType_JFHQ,
    CellType_WLBL,
    CellType_PJYJY,
    CellType_SZ,
};

@interface NewPersonalCenterViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic)UITableView *infoTableView;
@property (strong,nonatomic)NewPersonalCenterHeaderView *headerView;
@property (strong,nonatomic)NSArray<NSArray<NSNumber *> *> *cellTypes;
@end

@implementation NewPersonalCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cellTypes = @[
        @[@(CellType_DB), @(CellType_RMTFM), @(CellType_WDXX)],
        @[@(CellType_FLSC), @(CellType_WDDD), @(CellType_JFHQ)],
        @[@(CellType_WLBL), @(CellType_PJYJY), @(CellType_SZ)]
    ];
    
    self.infoTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    if (@available(iOS 11.0, *)) {
        self.infoTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.infoTableView.contentInset = UIEdgeInsetsZero;
    [self.infoTableView registerNib:[UINib nibWithNibName:@"NewPersonalCenterCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewPersonalCenterCell"];
    self.infoTableView.dataSource = self;
    self.infoTableView.delegate = self;
    
    [self.view addSubview:self.infoTableView];
    [self.infoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.infoTableView.superview);
        make.top.equalTo(self.infoTableView.superview);
    }];
    
    self.headerView = [NewPersonalCenterHeaderView headerView];
    
    __weak typeof (self) weakSelf = self;
    self.headerView.iconTapHandle = ^{
        [weakSelf myUserInfo];
    };
    [self.headerView setCoverTapHandle:^(NSString *url) {
        NewsWebViewController *controller = [NewsWebViewController new];
        controller.hasClose = YES;
        controller.urlString = url;
        [weakSelf showViewController:controller sender:nil];
    }];
    [self.headerView setItemTapHandle:^(NSInteger index) {
        switch (index) {
            case 0:
                [weakSelf actionZJ];
                break;
            case 1:
                [weakSelf actionPL];
                break;
            case 2:
                [weakSelf actionSC];
                break;
            case 3:
                [weakSelf actionQD];
                break;
            default: break;
        }
    }];
    
    //设置封面
    [ServiceUser doGetUserCenterCover:^(id responseObject, NSString *message) {
        [weakSelf.headerView refreshCover:responseObject];
    } failure:^(NSString *message) {
        [weakSelf.headerView refreshCover:nil];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        weakSelf.infoTableView.tableHeaderView = weakSelf.headerView;
    });
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    __weak typeof (self) weakSelf = self;
    if ([AccountManager sharedAccountManager].f_isLogined) {
        [ServiceUser doGetUserInfo:[AccountManager sharedAccountManager].f_uid
                           success:^(id responseObject, NSString *message) {
                               [weakSelf.headerView refresh];

            //获取积分
            [ServiceUserNew doQueryScore:^(id  _Nonnull responseObject, NSString * _Nonnull message) {
                [weakSelf.headerView refreshScore:responseObject];
            } failure:^(NSString * _Nonnull message) {
            }];

         } failure:^(NSString *message) {
             [weakSelf.headerView refresh];

             //获取积分
             [ServiceUserNew doQueryScore:^(id  _Nonnull responseObject, NSString * _Nonnull message) {
                 [weakSelf.headerView refreshScore:responseObject];
             } failure:^(NSString * _Nonnull message) {
             }];
         }];
    }
    else {
        [self.headerView refresh];
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cellTypes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellTypes[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewPersonalCenterCell *cell = (NewPersonalCenterCell *)[tableView dequeueReusableCellWithIdentifier:@"NewPersonalCenterCell"];
    CellType type = (CellType)self.cellTypes[indexPath.section][indexPath.row].integerValue;
    switch (type) {
        case CellType_DB:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_db"];
            cell.labelTitle.text = @"订报";
            break;
        case CellType_RMTFM:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_rmtfm"];
            cell.labelTitle.text = @"融媒体封面";
            break;
        case CellType_WDXX:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_wdxx"];
            cell.labelTitle.text = @"我的消息";
            break;
        case CellType_FLSC:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_flsc"];
            cell.labelTitle.text = @"福利商城";
            break;
        case CellType_WDDD:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_wddd"];
            cell.labelTitle.text = @"我的订单";
            break;
        case CellType_JFHQ:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_jfhq"];
            cell.labelTitle.text = @"积分换券";
            break;
        case CellType_WLBL:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_jfhq"];
            cell.labelTitle.text = @"网络报料";
            break;
        case CellType_PJYJY:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_pjyjy"];
            cell.labelTitle.text = @"评报与建议";
            break;
        case CellType_SZ:
            cell.imageViewIcon.image = [UIImage imageNamed:@"icon_cell_sz"];
            cell.labelTitle.text = @"设置";
            break;
        default:
            cell.imageViewIcon.image = nil;
            cell.labelTitle.text = @"";
            break;
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    CellType type = (CellType)self.cellTypes[indexPath.section][indexPath.row].integerValue;
    switch (type) {
        case CellType_DB:
            [self myOrder];
            break;
        case CellType_RMTFM: {
                AppDelegate *delegate_ = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                [delegate_ showOpenView];
            }
            break;
        case CellType_WDXX:
            [self myMessage];
            break;
        case CellType_FLSC:
            if ([self isLogin]) {
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hasClose = YES;
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = @"http://store.abcvote.cn/exlogin/0";
                controller.hasShare = NO;
                [self showViewController:controller sender:nil];
            }
            break;
        case CellType_WDDD:
            if ([self isLogin]) {
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hasClose = YES;
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = @"http://store.abcvote.cn/exlogin/1";
                controller.hasShare = NO;
                [self showViewController:controller sender:nil];
            }
            break;
        case CellType_JFHQ:
            if ([self isLogin]) {
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hasClose = YES;
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = @"http://store.abcvote.cn/exlogin/3";
                controller.hasShare = NO;
                [self showViewController:controller sender:nil];
            }
            break;
        case CellType_WLBL: {
            ReportListViewController *controller = [[ReportListViewController alloc] initWithNibName:@"ReportListViewController" bundle:[NSBundle mainBundle]];
            controller.hidesBottomBarWhenPushed = YES;
            [self showViewController:controller sender:nil];
        }
            break;
        case CellType_PJYJY:
            [self myFeedback];
            break;
        case CellType_SZ:
            [self mySet];
            break;
        default: break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (@available(iOS 11.0, *)) {
         return CGFLOAT_MIN;
     } else {
         return 2;
     }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (@available(iOS 11.0, *)) {
        return CGFLOAT_MIN;
     } else {
        return 2;
     }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section {
    return 5.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

#pragma mark 用户信息
- (void)myUserInfo {
    if ([self isLogin]) {
        //2016-6-22 hold left menu
        [AppDelegate sharedAppDelegate].myOpen = @"1";
        
        UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
        PersonalEditInformationViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalEditInformationViewController"];
        controller.hidesBottomBarWhenPushed = YES;
        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark 足迹
- (void)actionZJ {
    if ([self isLogin]) {
        MyHistoryViewController *controller = [MyHistoryViewController new];
        [self.navigationController pushViewController: controller animated:YES];
    }
}

#pragma mark 评论
- (void)actionPL {
    if ([self isLogin]) {
        [AppDelegate sharedAppDelegate].myOpen = @"1";
        MyCommentsViewController *controller = [MyCommentsViewController new];
        controller.hidesBottomBarWhenPushed = YES;
        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark 收藏
- (void)actionSC {
    if ([self isLogin]) {
        MyLikeViewController *controller = [MyLikeViewController new];
        [self.navigationController pushViewController: controller animated:YES];
    }
}

#pragma mark 签到
- (void)actionQD {
    if ([self isLogin]) {
        NewsWebViewController *controller = [NewsWebViewController new];
        controller.hasClose = YES;
        controller.hidesBottomBarWhenPushed = YES;
        controller.urlString = @"http://store.abcvote.cn/exlogin/2";
        controller.hasShare = NO;
        [self showViewController:controller sender:nil];
    }
}

#pragma mark 订报
- (void)myOrder {
    NewsWebViewController *controller = [NewsWebViewController new];
    controller.hasClose = YES;
    controller.hidesBottomBarWhenPushed = YES;
    controller.urlString = @"https://detail.youzan.com/show/goods?alias=2xbfxodfu3kr4";
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

#pragma mark 我的消息
- (void)myMessage {
    if ([self isLogin]) {
        //2016-6-22 hold left menu
        [AppDelegate sharedAppDelegate].myOpen = @"1";
        MyMessageViewController *controller = [MyMessageViewController new];
        controller.hidesBottomBarWhenPushed = YES;
        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark 我的福利
- (void)myWelfare {
    
    if (![AccountManager sharedAccountManager].f_isLogined) {
        
        //请绑定手机号 请先登录，领取福利须绑定手机号
        [UIAlertView showWithTitle:@"提示" message:@"请先登录，领取福利须绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1)
            {
                //2016-6-22 hold left menu
                [AppDelegate sharedAppDelegate].myOpen = @"1";
                //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                
                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                controller.hidesBottomBarWhenPushed = YES;
                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                
            }
        }];
        
        return;
        
    }
    
    if (![AccountManager sharedAccountManager].f_phone_num || [[AccountManager sharedAccountManager].f_phone_num isEqualToString:@""]) {
        //请绑定手机号 请先登录，领取福利须绑定手机号
        [UIAlertView showWithTitle:@"提示" message:@"请绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1)
            {
                //2016-6-22 hold left menu
                [AppDelegate sharedAppDelegate].myOpen = @"1";
                //绑定手机号
                //                                [[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                //
                //                                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                //                                PersonalEditInformationViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalEditInformationViewController"];
                //                                controller.hidesBottomBarWhenPushed = YES;
                //
                //                                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                
                //显示模态窗体
                ModalViewController *mvc =  [[ModalViewController alloc] init];
                mvc.delegate = self;
                [mvc initStyle];
                
                [[[UIApplication sharedApplication] visibleViewController].navigationController presentViewController:mvc animated:YES completion:^(void){
                    mvc.view.superview.backgroundColor = [UIColor clearColor];
                    
                }];
                
            }
        }];
        
        return;
    }
    
    //2016-6-22 hold left menu
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    //我的福利
    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
    
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    MyWelfareViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"MyWelfareViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

#pragma mark 推荐
- (void)myCommand {
    
    if (![AccountManager sharedAccountManager].f_isLogined) {
        //请绑定手机号 请先登录，领取福利须绑定手机号
        [UIAlertView showWithTitle:@"提示" message:@"请先登录" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1)
            {
                
                
                //2016-6-22 hold left menu
                [AppDelegate sharedAppDelegate].myOpen = @"1";
                
                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                controller.hidesBottomBarWhenPushed = YES;
                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
            }
        }];
        return;
    }
    
    if (![AccountManager sharedAccountManager].f_phone_num || [[AccountManager sharedAccountManager].f_phone_num isEqualToString:@""]) {
        [UIAlertView showWithTitle:@"提示" message:@"请绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1)
            {
                //2016-6-22 hold left menu
                [AppDelegate sharedAppDelegate].myOpen = @"1";
                //绑定手机号
                //                                [[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                //
                //                                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                //                                PersonalEditInformationViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalEditInformationViewController"];
                //                                controller.hidesBottomBarWhenPushed = YES;
                //                                controller.title=@"个人信息";
                //
                //                                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                //
                //显示模态窗体
                ModalViewController *mvc =  [[ModalViewController alloc] init];
                mvc.delegate = self;
                [mvc initStyle];
                
                [[[UIApplication sharedApplication] visibleViewController].navigationController presentViewController:mvc animated:YES completion:^(void){
                    mvc.view.superview.backgroundColor = [UIColor clearColor];
                    
                }];
                
            }
        }];
        
        return;
    }
    
    //分享推荐
    //2016-6-22 hold left menu
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    UIViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"MyShareRecommendViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

#pragma mark 反馈
- (void)myFeedback {
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    UIViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PublishFeedBackViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

#pragma mark 热线
- (void)myTel {
    HotlineViewController *controller = [[HotlineViewController alloc] initWithNibName:@"HotlineViewController" bundle:[NSBundle mainBundle]];
    [self showViewController:controller sender:nil];
}

#pragma mark 设置
- (void)mySet {
    //2016-6-22 hold left menu
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    UIViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalSetUpContainerViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

#pragma mark 检查登录状态
- (BOOL)isLogin {
    if (![AccountManager sharedAccountManager].f_isLogined) {
        // 请绑定手机号 请先登录，领取福利须绑定手机号
        [UIAlertView showWithTitle:@"提示" message:@"请先登录" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1)
            {
                //2016-6-22 hold left menu
                [AppDelegate sharedAppDelegate].myOpen = @"1";
                
                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                controller.hidesBottomBarWhenPushed = YES;
                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
            }
        }];
    }
    
    return [AccountManager sharedAccountManager].f_isLogined;
}

@end
